#ifndef DRAWABLEANIM_H
#define DRAWABLEANIM_H

#include "drawable.hpp"
#include "sprite.hpp"
#include <resource_cache/resource_cache.hpp>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <rapidxml/rapidxml.hpp>

class frame_sprite : public drawable
{
public:
	frame_sprite() : drawable() { }
	bool draw( sf::RenderWindow * renderWindow, float x = 0, float y = 0, float rotation = 0, float scale = 1 )
	{
		sprite* spr = dynamic_cast<sprite*>(m_sprite.get());
		if( spr != 0 )
		{
			spr->set_rect(m_rect);
			m_sprite->draw(renderWindow,b2Vec2(x,y),rotation,scale);
		}

		return true;
	}

	sf::IntRect  m_rect;
	drawable_ptr m_sprite;
};

class animation : public drawable
{
public:
	virtual bool draw( sf::RenderWindow * renderWindow, float x = 0, float y = 0, float rotation = 0, float scale = 1 );
	virtual void update( float delta_time );
	virtual void reset();

	bool	addFrame( drawable_ptr frame )
	{
		if( frame.use_count() == 0 )
			return false; 
		m_frames.push_back(frame); 
	}
	bool	addFrame( const std::string & filepath ); // sprite
	bool	addFrame( const std::string & filepath, int top, int left, int bottom, int right ); // subRect sprite

	animation( const std::string & filepath );
	bool loadFromFile( const std::string & filepath );
	animation() : drawable(), m_time_per_frame(0.125f), m_frame_delta(0), m_current_frame(0), m_repeat(true), m_reverse(false) { }
	virtual ~animation();

protected:
	std::vector<drawable_ptr> m_frames;
	std::vector<float> m_frame_times;
	bool loadAnimation( rapidxml::xml_node<> const * const animation_node );

	unsigned int m_current_frame;
	float m_time_per_frame;
	float m_frame_delta;
	bool  m_repeat;
	bool  m_reverse;

};

#endif
